﻿package com.webbei.myCustom.action;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import org.apache.struts2.ServletActionContext;

import com.webbei.myCustom.service.myCustomService;

public class saveSelectedPicture {
	private Integer x;
	private Integer y;
	private Integer w;
	private Integer h;
	private String pic;
	private String filepath;//截取后的图片路径
	private myCustomService mcs;
	public String execute(){
		System.out.println("进入saveSelectedPictureAction");
		System.out.println(x+","+y+","+w+","+h+","+pic);
		String[] t = pic.split("\\.");
		String filetype=t[t.length - 1];
		String path=ServletActionContext.getServletContext().getRealPath("/");
		String username=(String) ServletActionContext.getContext().getSession().get("username");
		filepath="img/resources/webbeiUserInfo/"+username+"/userTouxiang/"+String.valueOf((new Date().getTime()))+String.valueOf(new Random().nextInt(3)) + "."+filetype;
		FileInputStream is = null;
		ImageInputStream iis = null;
		try {
			int realwidth = ImageIO.read(new File(path+pic)).getWidth();
			System.out.println(realwidth);
			x=x*realwidth/300;
			y=y*realwidth/300;
			w=w*realwidth/300;
			h=h*realwidth/300;
			System.out.println(x+","+y+","+w+","+h);
			//原图片按比例缩放(realwidth:300)
			is = new FileInputStream(path+pic);
			Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(filetype);
			ImageReader reader = it.next();//返回注册ImageReader的Iterator，用于解码制定格式
			iis = ImageIO.createImageInputStream(is);// 获取图片流
			reader.setInput(iis, true);//读取数据流（源）。true:只向前搜索，避免之前已经读取的图像关联数据
			ImageReadParam param = reader.getDefaultReadParam();//对数据流进行解码
			Rectangle rect = new Rectangle(x, y, w, h);//创建一个矩形
			param.setSourceRegion(rect);//讲数据源目标设为选定的矩形
			BufferedImage bi = reader.read(0, param);//索引整个指定的对象（0 —— 对象），返回BufferedImage
			MyUtil.deleteFiles(path+"img/resources/webbeiUserInfo/"+username+"/userTouxiang");
			System.out.println("-----------------------------"+bi);
			System.out.println(filetype);
			System.out.println("-----------------------------"+path+filepath);
			ImageIO.write(bi, filetype, new File(path+filepath));//保存图片
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try{
				if (is != null) is.close();
				if (iis != null) iis.close();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		mcs.updateUserFace(username,filepath);
		return "success";
	}
	public void setMcs(myCustomService mcs) {
		this.mcs = mcs;
	}
	public String getPic() {
		return pic;
	}
	public void setPic(String pic) {
		this.pic = pic;
	}
	public Integer getX() {
		return x;
	}
	public void setX(Integer x) {
		this.x = x;
	}
	public Integer getY() {
		return y;
	}
	public void setY(Integer y) {
		this.y = y;
	}
	public Integer getW() {
		return w;
	}
	public void setW(Integer w) {
		this.w = w;
	}
	public Integer getH() {
		return h;
	}
	public void setH(Integer h) {
		this.h = h;
	}
	public String getFilepath() {
		return filepath;
	}
	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}
}
